package ru.volgogradetzzz.controls 
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	
	/**
	 * ...
	 * @author Volgogradetzzz
	 */
	public class AnimatedButton extends Sprite 
	{
		private const _HOLLOW:ColorTransform = new ColorTransform(0, 0, 0, 0);
		private var _bg:Sprite = new Sprite();
		
		private var _w:Number;
		private var _h:Number;
		
		private var _upState:MovieClip;
		private var _overState:MovieClip;
		private var _downState:MovieClip;
		private var _disabledState:MovieClip;
		
		private var _isMouseDown:Boolean = false;
		private var _enabled:Boolean = true;
		
		public function AnimatedButton(upState:MovieClip, overState:MovieClip, downState:MovieClip, disabledState:MovieClip) 
		{
			_upState = upState;
			_overState = overState;
			_downState = downState;
			_disabledState = disabledState;
			
			_upState.gotoAndStop(1);
			_overState.gotoAndStop(1);
			_downState.gotoAndStop(1);
			_disabledState.gotoAndStop(1);
			
			_w = upState.width;
			_h = upState.height;
			
			_upState.x = _overState.x = _downState.x = _disabledState.x = _w * 0.5;
			_upState.y = _overState.y = _downState.y = _disabledState.y = _h * 0.5;
			
			_upState.mouseChildren = _overState.mouseChildren = _downState.mouseChildren = _disabledState.mouseChildren = false;
			_upState.mouseEnabled = _overState.mouseEnabled = _downState.mouseEnabled = _disabledState.mouseEnabled = false;
			
			_overState.visible = _downState.visible = _disabledState.visible = false;
			
			_bg.graphics.beginFill(0x000000, 0);
			_bg.graphics.drawRect(0, 0, _w, _h);
			_bg.graphics.endFill();
			
			addChildAt(_bg, 0);
			addChild(_upState);
			addChild(_overState);
			addChild(_downState);
			addChild(_disabledState);
			
			_bg.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
			_bg.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
			_bg.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
		}
		
		public function setHitArea(bg:Sprite):void
		{
			_bg.removeEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
			_bg.removeEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
			_bg.removeEventListener(MouseEvent.MOUSE_DOWN, downHandler);
			removeChild(_bg);
			
			addChild(bg);
			bg.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
			bg.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
			bg.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
			bg.x = bg.y = 0;
			bg.transform.colorTransform = _HOLLOW;
		}
		
		public function set enabled(value:Boolean):void
		{
			if (_enabled == value) return;
			
			_enabled = value;
			
			if (!_enabled)
			{
				removeEventListener(Event.ENTER_FRAME, overEnterFrameHandler);
				removeEventListener(Event.ENTER_FRAME, overEnterFrameHandler);
				if(stage) stage.removeEventListener(MouseEvent.MOUSE_UP, stageUpHandler);
				
				_isMouseDown = false;
				
				_upState.visible = _overState.visible = _downState.visible = false;
				_disabledState.visible = true;
				_disabledState.gotoAndPlay(1);
			}
			else
			{
				_disabledState.visible = false;
				_disabledState.stop();
				
				if (mouseX > 0 && mouseX < _w && mouseY > 0 && mouseY < _h)
				{
					_overState.visible = true;
					_overState.gotoAndPlay(1);
				}
				else
				{
					_upState.visible = true;
					addEventListener(Event.ENTER_FRAME, outEnterFrameHandler);
				}
			}
		}
		
		public function get enabled():Boolean
		{
			return _enabled;
		}
		
		override public function get width():Number
		{
			return _w;
		}
		
		override public function set width(value:Number):void 
		{
			_w = value;
		}
		
		override public function get height():Number
		{
			return _h;
		}
		
		override public function set height(value:Number):void 
		{
			_h = value;
		}
		
		private function rollOverHandler(event:MouseEvent):void 
		{
			if (!enabled) return;
			
			addEventListener(Event.ENTER_FRAME, overEnterFrameHandler);
			_upState.play();
		}
		
		private function overEnterFrameHandler(event:Event):void 
		{
			if (_upState.currentFrame == _upState.totalFrames)
			{
				removeEventListener(Event.ENTER_FRAME, overEnterFrameHandler);
				
				_upState.stop();
				_upState.visible = false;
				
				_overState.visible = true;
				_overState.gotoAndPlay(1);
			}
		}
		
		private function rollOutHandler(event:MouseEvent):void 
		{
			if (!enabled) return;
			
			if (!_isMouseDown)
			{
				removeEventListener(Event.ENTER_FRAME, overEnterFrameHandler);
				
				_overState.visible = false;
				_overState.stop();
				
				_upState.visible = true;
				addEventListener(Event.ENTER_FRAME, outEnterFrameHandler);
			}
		}
		
		private function outEnterFrameHandler(event:Event):void 
		{
			_upState.prevFrame();
			if (_upState.currentFrame == 1)
			{
				_upState.stop();
				removeEventListener(Event.ENTER_FRAME,outEnterFrameHandler);
			}
		}
		
		private function downHandler(event:MouseEvent):void 
		{
			if (!enabled) return;
			
			removeEventListener(Event.ENTER_FRAME, overEnterFrameHandler);
			
			_upState.stop();
			_upState.visible = false;
			
			_overState.visible = false;
			_overState.stop();
			
			_downState.visible = true;
			_downState.gotoAndPlay(1);
			
			_isMouseDown = true;
			
			stage.addEventListener(MouseEvent.MOUSE_UP, stageUpHandler);
		}
		
		private function stageUpHandler(event:MouseEvent):void 
		{
			stage.removeEventListener(MouseEvent.MOUSE_UP, stageUpHandler);
			
			_isMouseDown = false;
			
			_downState.visible = false;
			_downState.stop();
			
			if (mouseX > 0 && mouseX < _w && mouseY > 0 && mouseY < _h)
			{
				_overState.visible = true;
				_overState.gotoAndPlay(1);
			}
			else
			{
				_upState.visible = true;
				addEventListener(Event.ENTER_FRAME, outEnterFrameHandler);
			}
		}
	}
}